package com.eims.sls_app.ui.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.eims.sls_app.dataaccess.AppGateway;
import com.eims.sls_app.ui.FollowInfoActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import bls.juejin.cn.R;

public class AppGatewayAdapter extends BaseAdapter {

	private List<AppGateway> listSchemes;
	private FollowInfoActivity followInfoActivity;

	public AppGatewayAdapter(FollowInfoActivity followInfoActivity,
			List<AppGateway> params) {
		this.followInfoActivity = followInfoActivity;
		setListSchemes(params);
	}

	public void setListSchemes(List<AppGateway> params) {
		if (params != null) {
			if (listSchemes == null) {
				listSchemes = new ArrayList<AppGateway>();
			} else {
				listSchemes.clear();
			}
			for (AppGateway numbers : params) {
				listSchemes.add(numbers);
			}
		}
	}

	public List<AppGateway> getListSchemes() {
		return listSchemes;
	}

	public void add(AppGateway appGateway) {
		listSchemes.add(appGateway);
	}

	public void add(int position, AppGateway appGateway) {
		listSchemes.add(position, appGateway);
	}

	public void remove(int index) {
		listSchemes.remove(index);
	}

	public void clear() {
		listSchemes.clear();
	}

	@Override
	public int getCount() {
		return listSchemes.size();
	}

	@Override
	public Object getItem(int position) {
		return listSchemes.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(followInfoActivity).inflate(
					R.layout.app_gateway_item, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (listSchemes.size() == 0) {
			return convertView;
		}
		final int index = position;
		AppGateway num = listSchemes.get(position);
		holder.tv1.setText(position + 1 + "");
		holder.tv2.setText(num.getName());
		holder.tv3.setText(num.getDetailMoney());
		String[] split = num.getDateTime().split(" ");
		String substring = split[1].substring(0, 8);
		holder.tv4.setText(substring);
		return convertView;
	}

	static class ViewHolder {

		TextView tv1, tv2, tv3, tv4;

		public ViewHolder(View view) {
			tv1 = (TextView) view.findViewById(R.id.tv1);
			tv2 = (TextView) view.findViewById(R.id.tv2);
			tv3 = (TextView) view.findViewById(R.id.tv3);
			tv4 = (TextView) view.findViewById(R.id.tv4);
		}
	}
}
